﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using WMS.HelperClasses;

namespace WMS.Reports
{
    public partial class DetailedOutpotReportFromTo : Form
    {
        public DetailedOutpotReportFromTo()
        {
            InitializeComponent();
            EnableDisableControls();
        }

        private void DetailedOutpotReportFromTo_Load(object sender, EventArgs e)
        {

        }

        private void BtnFromStoreClick(object sender, EventArgs e)
        {
            var searchForm = new fromGeneralEntityPicker("Store");
            if (searchForm.ShowDialog() == DialogResult.OK)
            {
                txtFromStoreCode.Text = searchForm.SelectedItemCode;
            }
        }

        private void BtnToStoreClick(object sender, EventArgs e)
        {
            var searchForm = new fromGeneralEntityPicker("Store");
            if (searchForm.ShowDialog() == DialogResult.OK)
            {
                txtToStoreCode.Text = searchForm.SelectedItemCode;
            }
        }

        private void BtnFromProductClick(object sender, EventArgs e)
        {
            var searchForm = new fromGeneralEntityPicker("Item");
            if (searchForm.ShowDialog() == DialogResult.OK)
            {
                txtFromProductCode.Text = searchForm.SelectedItemCode;
            }
        }

        private void BtnToProductClick(object sender, EventArgs e)
        {
            var searchForm = new fromGeneralEntityPicker("Item");
            if (searchForm.ShowDialog() == DialogResult.OK)
            {
                txtToProductCode.Text = searchForm.SelectedItemCode;
            }
        }

        private void BtnFromContractorClick(object sender, EventArgs e)
        {
            var searchForm = new fromGeneralEntityPicker("Contractor");
            if (searchForm.ShowDialog() == DialogResult.OK)
            {
                txtFromContractor.Text = searchForm.SelectedItemCode;
            }
        }

        private void BtnToContractorClick(object sender, EventArgs e)
        {
            var searchForm = new fromGeneralEntityPicker("Contractor");
            if (searchForm.ShowDialog() == DialogResult.OK)
            {
                txtToContractor.Text = searchForm.SelectedItemCode;
            }
        }

        private void TxtFromStoreCodeTextChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtFromStoreCode.Text))
            {
                DataTable DTStore = Business.GetStoreDetails(txtFromStoreCode.Text);
                if (DTStore.Rows.Count > 0)
                {
                    lblFromStoreName.Text = DTStore.Rows[0][2].ToString();
                }
                else
                {
                    lblFromStoreName.Text = string.Empty;
                }
            }
            EnableDisableControls();
        }

        private void TxtToStoreCodeTextChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtToStoreCode.Text))
            {
                DataTable DTStore = Business.GetStoreDetails(txtToStoreCode.Text);
                if (DTStore.Rows.Count > 0)
                {
                    lblToStoreName.Text = DTStore.Rows[0][2].ToString();
                }
                else
                {
                    lblToStoreName.Text = string.Empty;
                }
            }
            EnableDisableControls();
        }

        private void TxtFromProductCodeTextChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtFromProductCode.Text))
            {
                DataTable DTProduct = Business.GetItemDetails(txtFromProductCode.Text);
                if (DTProduct.Rows.Count > 0)
                {
                    lblFromProductName.Text = DTProduct.Rows[0][1].ToString();
                }
                else
                {
                    lblFromProductName.Text = string.Empty;
                }
            }
            EnableDisableControls();
        }

        private void TxtToProductCodeTextChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtToProductCode.Text))
            {
                DataTable DTProduct = Business.GetItemDetails(txtToProductCode.Text);
                if (DTProduct.Rows.Count > 0)
                {
                    lblToProductName.Text = DTProduct.Rows[0][1].ToString();
                }
                else
                {
                    lblToProductName.Text = string.Empty;
                }
            }
            EnableDisableControls();
        }

        private void TxtFromContractorTextChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtFromContractor.Text))
            {
                DataTable DTProduct = Business.GetContractorDetails(txtFromContractor.Text);
                if (DTProduct.Rows.Count > 0)
                {
                    lblFromContractorName.Text = DTProduct.Rows[0][2].ToString();
                }
                else
                {
                    lblFromContractorName.Text = string.Empty;
                }
            }
            EnableDisableControls();
        }

        private void TxtToContractorTextChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtToContractor.Text))
            {
                DataTable DTProduct = Business.GetContractorDetails(txtToContractor.Text);
                if (DTProduct.Rows.Count > 0)
                {
                    lblToContractorName.Text = DTProduct.Rows[0][2].ToString();
                }
                else
                {
                    lblToContractorName.Text = string.Empty;
                }
            }
            EnableDisableControls();
        }

        private void EnableDisableControls()
        {
            btnRun.Enabled = !string.IsNullOrEmpty(lblFromProductName.Text) &&
                             !string.IsNullOrEmpty(lblToProductName.Text) &&
                             !string.IsNullOrEmpty(lblFromStoreName.Text) &&
                             !string.IsNullOrEmpty(lblToStoreName.Text) &&
                             !string.IsNullOrEmpty(lblFromContractorName.Text) &&
                             !string.IsNullOrEmpty(lblToContractorName.Text) &&
                             !string.IsNullOrEmpty(txtFromProductCode.Text) &&
                             !string.IsNullOrEmpty(txtToProductCode.Text) &&
                             !string.IsNullOrEmpty(txtFromStoreCode.Text) &&
                             !string.IsNullOrEmpty(txtToStoreCode.Text) &&
                             !string.IsNullOrEmpty(txtFromContractor.Text) &&
                             !string.IsNullOrEmpty(txtToContractor.Text);
        }

        private void BtnRunClick(object sender, EventArgs e)
        {
            bindingSource1.DataSource = Business.GetDetailedIssuedFromTo(txtFromStoreCode.Text, txtToStoreCode.Text,
                                                                         txtFromProductCode.Text, txtToProductCode.Text,
                                                                         txtFromContractor.Text, txtToContractor.Text,
                                                                         datePickerFrom.Value, datePickerTo.Value);

            this.reportViewer1.LocalReport.DataSources.Clear();
            this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", bindingSource1));

            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("FromStore", txtFromStoreCode.Text));
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("ToStore", txtToStoreCode.Text));

            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("FromProduct", txtFromProductCode.Text));
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("ToProduct", txtToProductCode.Text));

            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("FromContractor", txtFromContractor.Text));
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("ToContractor", txtToContractor.Text));

            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("FromDate", datePickerFrom.Value.ToShortDateString()));
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("ToDate", datePickerTo.Value.ToShortDateString()));

            this.reportViewer1.RefreshReport();
        }
    }
}
